<?php
/**
 * Created by PhpStorm.
 * User: PC
 * Date: 2017/6/21
 * Time: 15:03
 */

namespace frontend\controllers;

use common\models\ActivityBargain;
use common\models\BargainRecord;
use common\models\Goods;
use common\models\ShopOrder;
use common\models\UserAddress;
use yii;

class ShopUserActivityController extends BaseApiController
{
    protected $activity_lock_time;
    protected $activity_long_lock_time;
    protected $activity_free_type_code;
    protected $activity_pay_type_code;
    protected $bargain_order_code;
    protected $order_pay_code;
    protected $activity_exchange_code;
    protected $order_ship_code;
    protected $activity_complete_code;
    protected $spent_huiwenbi_code;
    protected $received_huiwenbi_code;
    protected $bargain_order_type_code;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->activity_free_type_code = 0;
        $this->activity_pay_type_code  = 1;

        $this->activity_lock_time = 30;                 //砍价成功后商品锁定的时间 s
        $this->activity_long_lock_time = 1*60;         //兑换后商品锁定的时间 s

        $this->bargain_order_code = 1;
        $this->order_pay_code = 1;
        $this->activity_exchange_code = 2;
        $this->order_ship_code = 3;

        $this->activity_complete_code = 4;

        $this->spent_huiwenbi_code = 2;
        $this->received_huiwenbi_code = 1;

        $this->bargain_order_type_code = 1;
    }

    /**
     * 参加活动（砍价帮
     */
    public function actionBargain(){
        $activity_id = yii::$app->request->post('activity_id', 0);
        if(!$activity_id) $this->_errorData('1000', '参数错误');

        $activity_info = ActivityBargain::findOne($activity_id);
        if(!$activity_info) $this->_errorData('1001', '参数错误');

        if($activity_info['lock_status'] == 4){
            $this->_successData('商品已被人兑换');
        }elseif($activity_info['lock_status'] == 1 && (($activity_info['lock_time'] + $this->activity_lock_time) > time())){
            //砍价锁定时间
            $this->_errorData('10011', '此活动已锁定，请等待');
        }else if($activity_info['lock_status'] == 2 && (($activity_info['lock_time'] + $this->activity_long_lock_time) > time())){
            //点击了兑换后
            $this->_errorData('10012', '此活动已锁定，请等待');
        }else{
            //已过期 去掉锁
            if($activity_info['lock_status'] != 0){
                $unlock_ret = ActivityBargain::updateAll(['lock_status'=>0, 'lock_time'=>'0', 'user_id'=>'0'], ['activity_id'=>$activity_info['activity_id']]);
                if(!$unlock_ret) $this->_errorData('10012', '解锁失败');
            }
        }

        $user_info = $this->_getUserModel();

        //计算汇闻币是否足够
        if($user_info->amount < $activity_info['huiwenbi']) $this->_errorData('1007', '汇闻币不足');

        //活动结束时间
        if(strtotime($activity_info['end_time']) < time()) $this->_errorData('1002', '此活动已结束');

        $today_start_time = strtotime(date('Y-m-d', time()));
        $today_end_time = $today_start_time + 24 * 3600 - 1;
        //类型 0:每天参与次数  1 活动时间内参与次数
        if($activity_info['free_num_type'] == $this->activity_free_type_code) {
            /* 当天参与的免费次数 */
            $free_count = BargainRecord::find()
                ->where(['activity_id' => $activity_id, 'user_id' => $user_info->user_id, 'type' => $this->activity_free_type_code])
                ->andWhere(['>', 'create_time', $today_start_time])
                ->andWhere(['<', 'create_time', $today_end_time])
                ->count();
        }elseif($activity_info['free_num_type'] == $this->activity_pay_type_code){
            /* 活动期间还有免费次数 */
            $free_count = BargainRecord::find()
                ->where(['activity_id' => $activity_id, 'user_id' => $user_info->user_id, 'type' => $this->activity_free_type_code])
                ->count();
        }else{
            $free_count = 0;
        }

        if ($activity_info['free_num'] > $free_count) {
            $prize = $this->_getPrize($activity_info, $user_info, $this->activity_free_type_code);
            $this->_successData($prize);
        }else{
            /* 没有免费次数 开始计算付费次数 */
            if ($activity_info['pay_num_type'] == 0) {
                /* 如果付费次数是按照每天计算 */
                $user_today_pay_count = BargainRecord::find()
                    ->where(['activity_id' => $activity_id, 'user_id' => $user_info->user_id, 'type' => $this->activity_pay_type_code])
                    ->andWhere(['>', 'create_time', $today_start_time])
                    ->andWhere(['<', 'create_time', $today_end_time])
                    ->count();
                if ($user_today_pay_count >= $activity_info['pay_num']) $this->_errorData('1003', '今日次数已用完');
            }elseif ($activity_info['pay_num_type'] == 1){
                /* 如果付费次数是按照活动时间计算 */
                $user_pay_count = BargainRecord::find()
                    ->where(['activity_id' => $activity_id, 'user_id' => $user_info->user_id, 'type' => $this->activity_pay_type_code])
                    ->count();
                if ($user_pay_count >= $activity_info['pay_num']) $this->_errorData('1003', '次数已用完');
            }

            /* 获取砍价 */
            $prize = $this->_getPrize($activity_info, $user_info, $this->activity_pay_type_code);
            $this->_successData($prize);
        }
    }

    private function _getPrize($activity_info, $user_info, $pay_type = 0)
    {
        //奖池中的汇闻币
        $pool_huiwenbi = BargainRecord::find()->where(['activity_id' => $activity_info['activity_id']])->sum('huiwenbi');

        //奖品信息 商品信息
        $goods_info = Goods::findOne($activity_info['goods_id'])->toArray();

        //计算砍多少钱 5000*10%*（1-（0-50%））
        $bargian_price = $activity_info['bargain_price'] * 0.1 * (1 - (0 - mt_rand(0, 50) / 100));
        $bargian_price = mt_rand(0, $bargian_price);

        //保底价
        if($activity_info['bargain_price'] - $bargian_price < $activity_info['reserve_price']) return 0;

        if($pay_type == $this->activity_free_type_code){
            if($pool_huiwenbi > $bargian_price){
                /* 免费机会 奖池够 添加砍价记录 锁定活动 */
                $trans = yii::$app->db->beginTransaction();
                try {
                    $insert_record_sql = "INSERT INTO vrshop.bargain_record VALUES ('', '{$activity_info['activity_id']}', '{$user_info->user_id}', '0', '{$bargian_price}','{$this->activity_free_type_code}', UNIX_TIMESTAMP())";
                    $insert_record_ret = yii::$app->db->createCommand($insert_record_sql)->execute();
                    if (!$insert_record_ret) throw new \Exception('插入记录失败');

                    //减去商品价格
                    $set_bargain_price_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain',
                        [
                            'bargain_price' => new yii\db\Expression("bargain_price - {$bargian_price}"),
                            'lock_status' => '1',
                            'user_id' => $user_info->user_id,
                            'lock_time' => time(),
                        ],
                        'activity_id = '.$activity_info['activity_id']
                    )->execute();
                    if(!$set_bargain_price_ret) throw new \Exception('错误，请稍后重试');

                    $trans->commit();
                }catch (\Exception $e){
                    $trans->rollBack();
                    return $e->getMessage();
                }
            }else{
                /* 免费机会 奖池不够 添加砍价记录 锁定活动*/
                $trans = yii::$app->db->beginTransaction();
                try{
                    $insert_record_sql = "INSERT INTO vrshop.bargain_record VALUES ('', '{$activity_info['activity_id']}', '{$user_info->user_id}', '0', '0','{$this->activity_free_type_code}', UNIX_TIMESTAMP())";
                    $insert_record_ret = yii::$app->db->createCommand($insert_record_sql)->execute();
                    if(!$insert_record_ret) throw new \Exception('插入数据失败');

                    $up_activity_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain', ['lock_status'=>'1', 'lock_time'=>time(), 'user_id'=>$user_info->user_id], "activity_id = {$activity_info['activity_id']}")->execute();
                    if(!$up_activity_ret) throw new \Exception('更新数据失败');

                    $trans->commit();
                }catch (\Exception $e){
                    $trans->rollBack();
                    return $e->getMessage();
                }

                $bargian_price = 0;
            }
        }else if($pay_type == $this->activity_pay_type_code){
            if($pool_huiwenbi > $bargian_price){
                /* 付费机会 奖池够 添加砍价记录，扣除汇闻币 */
                $trans = yii::$app->db->beginTransaction();
                try {
                    $insert_record_sql = "INSERT INTO vrshop.bargain_record VALUES ('', '{$activity_info['activity_id']}', '{$user_info->user_id}', '{$activity_info['huiwenbi']}', '{$bargian_price}','{$this->activity_pay_type_code}', UNIX_TIMESTAMP())";
                    $insert_record_ret = yii::$app->db->createCommand($insert_record_sql)->execute();
                    if (!$insert_record_ret) throw new \Exception('插入记录失败');

                    /* 扣除汇闻币 */
                    $deduct_huiwenbi_sql = "UPDATE vruser1.user SET `amount` = amount - {$activity_info['huiwenbi']} WHERE `user_id` = {$user_info->user_id} AND `amount` > 0";
                    $deduct_huiwenbi_ret = yii::$app->db->createCommand($deduct_huiwenbi_sql)->execute();
                    if (!$deduct_huiwenbi_ret) throw new \Exception('扣除汇闻币失败');

                    //更新汇闻币明细
                    $surplus = (int)($user_info->amount - $activity_info['huiwenbi']);
                    $insert_user_amount_ret = yii::$app->db->createCommand()->insert('vruser1.user_amount',[
                        'user_id' => $user_info->user_id,
                        'operate_cnt' => $activity_info['huiwenbi'],
                        'surplus' => $surplus,
                        'operate' => $this->spent_huiwenbi_code,
                        'operate_name' => '参加砍价帮花费:'.$activity_info['activity_id'],
                        'created_at' => date('Y-m-d H:i:s', time()),
                    ])->execute();
                    if(!$insert_user_amount_ret) throw new \Exception('更新用户汇闻币明细错误');

                    //减去商品价格 锁定活动
                    $set_bargain_price_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain',
                        [
                            'bargain_price' => new yii\db\Expression("bargain_price - {$bargian_price}"),
                            'lock_status' => '1',
                            'user_id' => $user_info->user_id,
                            'lock_time' => time()
                        ],
                        'activity_id = '.$activity_info['activity_id']
                    )->execute();
                    if(!$set_bargain_price_ret) throw new \Exception('错误，请稍后重试');

                    $trans->commit();
                }catch (\Exception $e){
                    $trans->rollBack();
                    return $e->getMessage();
                }
            }else{
                /* 付费机会 奖池不够 砍掉的价格为零 添加砍价记录，扣除汇闻币*/
                $trans = yii::$app->db->beginTransaction();
                try {
                    $insert_record_sql = "INSERT INTO vrshop.bargain_record VALUES ('', '{$activity_info['activity_id']}', '{$user_info->user_id}', '{$activity_info['huiwenbi']}', '','{$this->activity_pay_type_code}', UNIX_TIMESTAMP())";
                    $insert_record_ret = yii::$app->db->createCommand($insert_record_sql)->execute();
                    if (!$insert_record_ret) throw new \Exception('插入记录失败');

                    /* 扣除汇闻币 */
                    $deduct_huiwenbi_sql = "UPDATE vruser1.user SET `amount` = amount - {$activity_info['huiwenbi']} WHERE `user_id` = {$user_info->user_id}";
                    $deduct_huiwenbi_ret = yii::$app->db->createCommand($deduct_huiwenbi_sql)->execute();
                    if (!$deduct_huiwenbi_ret) throw new \Exception('扣除汇闻币失败');

                    //更新汇闻币明细
                    $surplus = (int)($user_info->amount - $activity_info['huiwenbi']);
                    $insert_user_amount_ret = yii::$app->db->createCommand()->insert('vruser1.user_amount',[
                        'user_id' => $user_info->user_id,
                        'operate_cnt' => $activity_info['huiwenbi'],
                        'surplus' => $surplus,
                        'operate' => $this->spent_huiwenbi_code,
                        'operate_name' => '参加砍价帮花费:'.$activity_info['activity_id'],
                        'created_at' => date('Y-m-d H:i:s', time()),
                    ])->execute();
                    if(!$insert_user_amount_ret) throw new \Exception('更新用户汇闻币明细错误');

                    /* 锁定活动 */
                    $up_activity_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain', ['lock_status'=>'1', 'lock_time'=>time(), 'user_id'=>$user_info->user_id], "activity_id = {$activity_info['activity_id']}")->execute();

                    $trans->commit();
                }catch (\Exception $e){
                    $trans->rollBack();
                    return $e->getMessage();
                }
                $bargian_price = 0;
            }
        }
        return $bargian_price;
    }

    /**
     * 兑换杀价帮奖品（商品
     */
    public function actionActivityExchange(){
        $company_id  = yii::$app->request->post('company_id', 0);
        $activity_id = yii::$app->request->post('activity_id', 0);
        if(!$activity_id) $this->_errorData('2000', '参数错误');

        $user_info = $this->_getUserModel();
        if(!$user_info) $this->_errorData('2001', '参数错误');

        $activity_info = ActivityBargain::find()
            ->alias('ab')
            ->select(['ab.*', 'g.goods_name', 'g.banner_image', 'g.goods_type', 'g.delivery_area', 'g.freight_type', 'g.freight'])
            ->leftJoin('vrshop.goods g', 'ab.goods_id = g.goods_id')
            ->where(['ab.activity_id'=>$activity_id])
            ->asArray()->one();
        if($activity_info['user_id'] != $user_info->user_id) $this->_errorData('2002', '参数错误');
        if($activity_info['lock_time'] + $this->activity_lock_time < time()){
            ActivityBargain::updateAll(['lock_status'=>'0', 'lock_time'=>'0', 'user_id'=>'0'], ['activity_id'=>$activity_id]);
            $this->_errorData('2003', '兑换商品超时');
        }

        $up_lock_info = ActivityBargain::updateAll(['lock_status'=>2, 'lock_time'=>time()], ['activity_id'=>$activity_info['activity_id']]);
        if(!$up_lock_info) $this->_errorData('2003', '数据更新失败，请重试');

        /* 计算运费 */
        $address_info = UserAddress::find()->where(['user_id'=>$user_info->user_id, 'is_default'=>'1'])->asArray()->one();
        $freight = ShopOrder::getFreight($activity_info['company_id'], $activity_info, $address_info);
        $address_info['freight'] = $freight;

        $this->_successData($activity_info);
    }

    /**
     * 确认兑换奖品
     */
    public function actionConfirmExchange(){
        $company_id  = yii::$app->request->post('company_id', 0);
        $activity_id = yii::$app->request->post('activity_id', 0);
        $address_id  = yii::$app->request->post('address_id', 0);
        $freight     = yii::$app->request->post('freight', 0);
        $order_remarks = yii::$app->request->post('order_remarks', '');
        if(!$activity_id || !$address_id) $this->_errorData('3000', '参数错误');

        $activity_info = ActivityBargain::findOne($activity_id)->toArray();

        $user_info = $this->_getUserModel();
        if($user_info->amount - $activity_info['bargain_price'] < 0) $this->_errorData('30001', '汇闻币不足');

        if($activity_info['user_id'] != $user_info->user_id) $this->_errorData('3001', '参数错误');

        if($activity_info['lock_status'] == 1 || ($activity_info['lock_time'] + $this->activity_long_lock_time < time())) $this->_errorData('3003', '兑换商品超时');

        $trans = yii::$app->db->beginTransaction();
        try{
            $insert_order_ret = yii::$app->db->createCommand()->insert('vrshop.shop_order', [
                'company_id' => $activity_info['company_id'],
                'user_id'    => $user_info->user_id,
                'order_number' => ShopOrder::_createOsn($user_info->user_id, 0, 0),
                'huiwenbi' => $activity_info['bargain_price'],
                'reciver_id' => $address_id,
                'freight' => $freight,
                'order_type' => $this->bargain_order_type_code,
                'activity_id' => $activity_info['activity_id'],
                'status' => $this->order_pay_code,
                'order_remarks' => $order_remarks,
                'create_time' => time(),
            ])->execute();
            if(!$insert_order_ret) throw new \Exception('插入订单失败，请重试');

            $up_activity_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain', ['lock_status'=>$this->activity_complete_code], "activity_id = {$activity_info['activity_id']}")->execute();
            if(!$up_activity_ret) throw new \Exception('更新活动表失败，请重试');

            $up_user_ret = yii::$app->db->createCommand()->update('vruser1.user', ['amount'=>new yii\db\Expression("amount - {$activity_info['bargain_price']}")], "user_id = {$user_info->user_id}")->execute();
            if(!$up_user_ret) $this->_errorData('减汇闻币失败');

            $surplus = (int)($user_info - $activity_info['bargain_price']);
            $insert_user_amount_ret = yii::$app->db->createCommand()->insert('vruser1.user_amount',[
                'user_id' => $user_info->user_id,
                'operate_cnt' => $activity_info['bargain_price'],
                'surplus' => $surplus,
                'operate' => $this->spent_huiwenbi_code,
                'operate_name' => '兑换杀价帮奖品：'.$activity_id,
                'created_at' => date('Y-m-d H:i:s', time()),
            ])->execute();
            if(!$insert_user_amount_ret) throw new \Exception('更新用户汇闻币明细错误');

            $trans->commit();
            $this->_successData('兑换成功');
        }catch (\Exception $e){
            $trans->rollBack();
            $this->_errorData('3005', $e->getMessage());
        }
        $this->_errorData('3004', '兑换失败，请重试');
    }

    public function actionCancelBargainOrder(){
        $activity_id = yii::$app->request->post('activity_id', 0);
        $order_id    = yii::$app->request->post('order_id', 0);
        if(!$order_id) $this->_errorData('4000', '参数错误');

        $order_info = ShopOrder::findOne($order_id)->toArray();
        if($order_info['status'] >= $this->order_ship_code) $this->_errorData('4002', '已发货的商品不能申请退货');
        if($order_info['order_type'] != $this->bargain_order_code) $this->_errorData('4006', '不是杀价帮订单');
        if($order_info['activity_id'] == 0) $this->_errorData('4007', '订单信息错误');

        $user_info = $this->_getUserModel();
        $activity_info = ActivityBargain::findOne($order_info['activity_id'])->toArray();
        if($activity_info['user_id'] != $user_info->user_id) $this->_errorData('只有本人才可以取消订单');
        if($activity_info['lock_status'] != $this->activity_complete_code) $this->_errorData('4001', '数据错误');

        $trans = yii::$app->db->beginTransaction();
        try{
            $up_order_ret = yii::$app->db->createCommand()->update('vrshop.shop_order', ['status'=>0], "order_id = {$order_id}")->execute();
            if(!$up_order_ret) throw new \Exception('订单更新失败');

            $up_activity_ret = yii::$app->db->createCommand()->update('vrshop.activity_bargain', ['lock_status'=>'0', 'lock_time'=>'0', 'user_id'=>'0'], "activity_id = {$order_info['activity_id']}")->execute();
            if(!$up_activity_ret) throw new \Exception('活动更新失败');

            $up_user_ret = yii::$app->db->createCommand()->update('vruser1.user', ['amount'=>new yii\db\Expression("amount + {$activity_info['bargain_price']}")], "user_id = {$user_info->user_id}")->execute();
            if(!$up_user_ret) throw new \Exception('返还汇闻币失败');

            $surplus = (int)($user_info->amount + $activity_info['bargain_price']);
            $insert_user_amount_ret = yii::$app->db->createCommand()->insert('vruser1.user_amount', [
                'user_id' => $user_info->user_id,
                'operate_cnt' => $activity_info['bargain_price'],
                'surplus' => $surplus,
                'operate' => $this->received_huiwenbi_code,
                'operate_name' => '杀价帮奖品退款：'.$activity_id,
                'created_at' => date('Y-m-d H:i:s', time()),
            ])->execute();
            if(!$insert_user_amount_ret) throw new \Exception('更新用户汇闻币明细错误');

            $trans->commit();
            $this->_successData('订单取消成功');
        }catch(\Exception $e){
            $trans->rollBack();
            $this->_errorData('4004', $e->getMessage());
        }

        $this->_errorData('4005', '订单更新失败');
    }
}